import sys, os
import main
import cPickle as pickle

usage = "usage: python ./launcher.py [options]\n"
usage+= "    --help or -h : display this help text\n"
usage+= "    --wx : use wx GUI version\n"
usage+= "    --gtk or no option : use GTK GUI version\n"
usage+= "    --regenerate : regenerate galaxy (stored in dump.glx)\n"
usage+= "    --perf <high, avg, low>: high, average or low performance (if --perf is not set - high perf is used)"

NOT_SET = False
SET = True
HAS_OPTION = True
NO_OPTION = False

def parse_args(args):
    skip = False
    for i, arg in enumerate(sys.argv):
        if skip:
            skip = False
            continue
        if arg in args:
            if args[arg]["has_option"] == HAS_OPTION:
                try:
                    args[arg]["option"] = sys.argv[i+1]
                except:
                    print "expected option for argument", arg, ", but it doesnt exist"
                    print usage
                    exit()
                else:
                    args[arg]["is_set"] = SET
                    skip = True
            else:
                args[arg]["is_set"] = SET

if __name__ == '__main__':
    args = {"--regenerate": {"is_set": NOT_SET, "has_option": NO_OPTION, "option": None}, "--gtk": {"is_set": NOT_SET, "has_option": NO_OPTION, "option": None}, "--wx": {"is_set": NOT_SET, "has_option": NO_OPTION, "option": None}, "--help": {"is_set": NOT_SET, "has_option": NO_OPTION, "option": None}, "-h": {"is_set": NOT_SET, "has_option": NO_OPTION, "option": None}, "--perf": {"is_set": NOT_SET, "has_option": HAS_OPTION, "option": None}}
    parse_args(args)
    data = None
    if args["--help"]["is_set"] or args["-h"]["is_set"]:
        print usage
        exit()

    perf = 1 # high
    if args["--perf"]["is_set"]:
        if args["--perf"]["option"] == "high":
            perf = 1
        elif args["--perf"]["option"] == "avg":
            perf = 2
        elif args["--perf"]["option"] == "low":
            perf = 3

    if args["--regenerate"]["is_set"]:
        data = main.data_gen()
        dump = open("dump.glx", "wb")
        pickle.dump(data, dump)
        dump.close()
    else:
        try:
            dump = open("dump.glx", "rb")
        except:
            print "could not open file dump.glx for reading"
            print usage
            exit()
        else:
            data = pickle.load(dump)
            dump.close()
    if sys.platform == 'win32' and (args["--gtk"]["is_set"] == NOT_SET):
        sys.path.insert(0, ".")
        class UniStream(object): 
            __slots__= "fileno", "softspace", 
            def __init__(self, fileobject): 
                self.fileno= fileobject.fileno() 
                self.softspace= False 
            def write(self, text): 
                if isinstance(text, unicode): 
                    os.write(self.fileno, text.encode("utf_8")) 
                else: 
                    os.write(self.fileno, text) 
        sys.stdout= UniStream(sys.stdout) 
        sys.stderr= UniStream(sys.stderr) 

        import wxmain
        wxmain.start(data, perf)
    else:
        if args["--wx"]["is_set"] == SET:
            import wxmain
            wxmain.start(data, perf)
        else:
            import gtkmain
            gtkmain.start(data, perf)
